/*
 *  AMPPreviousButtonObserver.cpp
 *  AMPlugin
 *
 *  Created by Vu Tiet on 7/27/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"

// Interface includes
#include "ISubject.h"
#include "IControlView.h"
#include "ISelectionUtils.h"
#include "IStringData.h"
#include "IControlView.h"
#include "ITriStateControlData.h"
#include "IWidgetParent.h"
#include "IPanelControlData.h"
#include "ITextControlData.h"

// General includes
#include "CObserver.h"
#include "Trace.h"
#include "KeyStates.h"

// Project includes
#include "AMPID.h"
#include "Notifier.h"
#include "AMGlobal.h"


// Vu Tiet
#include <iostream>
using namespace std;


class AMPPreviousButtonObserver : public CObserver
	{
	public:
		/**
		 Constructor.
		 @param boss interface ptr from boss object on which this interface is aggregated.
		 */
		AMPPreviousButtonObserver( IPMUnknown* boss );
		
		/** Destructor
		 */
		virtual ~AMPPreviousButtonObserver() ;
		
		/** Attaches observed subjects. Observes the button's ITriStateControlData to catch clicks
		 and the IID_ISNIPRUNSNIPPETNAMESTRINGDATA to catch when the name
		 of the snippet to be run changes.
		 */
		virtual void AutoAttach();
		
		/** Detaches observed subjects.
		 */
		virtual void AutoDetach();
		
		/** An observed subject has changed.
		 */
		virtual void Update( const ClassID& theChange, ISubject* theSubject, const PMIID& protocol, void* changedBy ) ;
		
	private:
		/*
		 InterfaceID used to aggregate the the widget observer onto the panel.
		 */
		const PMIID fObserverIID;
	};

/* Make the implementation available to the application.
 */
CREATE_PMINTERFACE(AMPPreviousButtonObserver, kAMPkPreviousButtonObserverImpl)

/*
 */
AMPPreviousButtonObserver::AMPPreviousButtonObserver( IPMUnknown* boss ) :
CObserver( boss ), fObserverIID(IID_IPREVBUTTONOBSERVER)
{
}

/*
 */
AMPPreviousButtonObserver::~AMPPreviousButtonObserver()
{
}

/********************************************************************************************************************
 *****Function: AutoAttach
 *****Creator:  Vu Tiet
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void AMPPreviousButtonObserver::AutoAttach()
{
	do {
		InterfacePtr<ISubject> subject(this, UseDefaultIID());
		ASSERT(subject);
		if (!subject) {
			break;
		}
		subject->AttachObserver(this, ITriStateControlData::kDefaultIID, fObserverIID);
		//subject->AttachObserver(this, IID_ISNIPRUNSNIPPETNAMESTRINGDATA, fObserverIID);
	} while(false);
}

/********************************************************************************************************************
 *****Function: AutoDetach
 *****Creator:  Vu Tiet
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void AMPPreviousButtonObserver::AutoDetach()
{
	do {
		InterfacePtr<ISubject> subject(this, UseDefaultIID());
		ASSERT(subject);
		if (!subject) {
			break;
		}
		subject->DetachObserver(this, ITriStateControlData::kDefaultIID, fObserverIID);
		//subject->DetachObserver(this, IID_ISNIPRUNSNIPPETNAMESTRINGDATA, fObserverIID);
	} while(false);
}

/********************************************************************************************************************
 *****Function: Update
 *****Creator:  Vu Tiet
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void AMPPreviousButtonObserver::Update(const ClassID& theChange, ISubject* theSubject, const PMIID& protocol, void* changedBy )
{
	do {
		if (theChange == kTrueStateMessage)
		{
			// The button has been clicked
			//cout << "Previous Button clicked, send notification here" << endl;
			Notifier *notifier = Notifier::GetInstance();
			notifier->Notify(PREV_BTN_SIG);
		}
		else {
			//cout << "Whhat change of Previous is this????" << endl;
		}
	} while(false);
}

// End, AMPPreviousButtonObserver.cpp.